// pages/articles/articles.js
var api = require('../../utils/api.js');

Page({

  data: {
    list: [],
    pageIndex: 1,
    pageSize: 10,
    isHideLoadMore: true
  },

  onLoad: function (options) {
    var that = this;
    loadData(that, function (list) {
      that.setData({ list: list });
    });
  },

  onReachBottom: function () {
    var that = this;
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      isHideLoadMore: false
    });
    loadData(that, function (list) {
      that.setData({
        list: that.data.list.concat(list),
        isHideLoadMore: true
      });
    });
  },
  
  previewImage: function (e) {
    var that = this;
    var images = [];
    for (var i = 0; i < that.data.list.length; i++) {
      images[i] = that.data.list[i].img_url;
    }
    console.log(e);
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: images[index],
      urls: images

    })
  }

})

function loadData(that, fun) {
  api.loadArticles('pics', that.data.pageIndex, that.data.pageSize, function (list, count) {
    fun(list);
  });
}